﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using YJYpublicClass;
using System.ComponentModel;
using System.Threading;

namespace YJYpublicClass
{
   public class FJI_FSK_24_T4 : ILightControl
    {
        public bool Connection
        {
            get
            {
                return sport.IsOpen;
            }
        }
        public string Mes { get; set; }
        private SerialPort sport = new SerialPort();

        private object syncRoot = new object();

       // private static FJI_FSK_24_T4 instance = null;
        
        private Config cft = new Config();
        public ConfigBase cf {
            get { return cft; }
            set { cft = (Config)value; }
        }

        public void Close()
        {
            lock (syncRoot)
            {
                sport.Close();
            }
        }

        public bool CloseChx(int chNo)
        {
            string mes = string.Empty;
            SendCommand("SL#", out mes);
            if (mes != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Connect()
        {
            lock (syncRoot)
            {
                try
                {
                    if (sport.IsOpen)
                    {
                        sport.Close();
                    }
                    sport.PortName = "COM" + cft.port;
                    sport.BaudRate = cft.baudRate;
                    sport.StopBits = cft.stopBits;
                    sport.DataBits = cft.dataBits;
                    sport.ReadTimeout = cft.timeout;
                    sport.WriteTimeout = cft.timeout;
                    sport.Open();
                    sport.ReadExisting();
                }
                catch (Exception ex)
                {
                    Mes = ex.ToString();
                }
            }
        }

        public int[] GetValue()
        {
            throw new NotImplementedException();
        }

        public int GetValue(int chNo)
        {
            string mes = string.Empty;
            switch (chNo)
            {
                case 0:
                    SendCommand("SA#", out mes);
                    break;
                case 1:
                    SendCommand("SA#", out mes);
                    break;

            }
           if(mes!=string.Empty)
            {
                string value= mes.Substring(3);
                int valuei = Convert.ToInt32(value);
                return valuei;          
            }else
            {
                return 0;
            }
        }

        public bool IsExists()
        {
            if (!sport.IsOpen)
            {
                return false;
            }
            return true;
        }

        public bool OpenChx(int chNo)
        {
            string mes=string.Empty;
            SendCommand("SH#",out mes);
            if(mes!=string.Empty)
            {
                return true;
            }else
            {
                return false;
            }
        }

        public bool SetValue(int chNo, int val)
        {
            string mes = string.Empty;
            switch (chNo)
            {
                case 0:
                    SendCommand("SA"+ val.ToString()+"#", out mes);
                    break;
                case 1:
                    SendCommand("SB" + val.ToString() + "#", out mes);
                    break;

            }
            if(mes!= string.Empty)
            {
                return true;
                
            }else
            {
                return false;
            }
        }

        public bool SetValues(int[] channelArray, int[] values)
        {
            throw new NotImplementedException();
        }

        private bool SendChar(char ch)
        {
            sport.Write(new char[1] { ch }, 0, 1);
            char rch = (char)sport.ReadChar();
            return ch == rch;
        }

        private bool SendCommand(string cmd, out string ReCmd)
        {
            sport.WriteLine(cmd);
            Thread.Sleep(50);
            int n = sport.BytesToRead;
            Byte[] BUF = new byte[n];
            sport.Read(BUF, 0, n);
            ReCmd = System.Text.Encoding.UTF8.GetString(BUF);
            return true;
        }

        protected string ExecuteCommand(string cmd)
        {
            lock (syncRoot)
            {
                string Recmd = string.Empty;
                if (SendCommand(cmd, out Recmd))
                {
                    return Recmd.Trim().ToUpper();
                }
                return string.Empty;
            }
        }
    }
    public class Config : ConfigBase
    {
  
        [Category("串口参数"), Description("int类型"), DisplayName("端口号"), ReadOnly(false)]
        public int port { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("波特率"), ReadOnly(false)]
        public int baudRate { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("传输超时"), ReadOnly(false)]
        public int timeout { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("停止位"), ReadOnly(false)]
        public StopBits stopBits { get; set; }
        [Category("串口参数"), Description("int类型"), DisplayName("数据位"), ReadOnly(false)]
        public int dataBits { get; set; }

        [Category("光源控制"), Description("int类型"), DisplayName("通道选择"), ReadOnly(false)]
        public int chSel
        {
            get; set;
        }
        [Category("光源控制"), Description("int类型"), DisplayName("通道值"), ReadOnly(false)]
        public int chSelV
        {
            get; set;
        }
        public Config()
        {
            port = 1;
            baudRate = 19200;
            timeout = 2000;
            stopBits = StopBits.One;
            dataBits = 8;
            chSel = 1;
            chSelV = 200;
        }


}


}
